﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AForge.Video.DirectShow;

namespace NewCapturerDemo
{
    public class CamProperties
    {
        private VideoCaptureDevice _camera;

        public CamProperties(VideoCaptureDevice camera)
        {
            _camera = camera;
            InitPoperties();
        }

        public CamProperties()
        {
            InitPoperties();
        }

        private Dictionary<CamProc, PropPair> Properties = new Dictionary<CamProc, PropPair>();

        public void InitPoperties(CamProc[] array)
        {
            Properties.Clear();
            if (array == null || array.Length == 0)
            {
                return;
            }
            foreach (CamProc p in array)
            {
                Properties.Add(p, new PropPair(p.ToString()));
            }
        }

        public void LoadPopertiesFromIni()
        {
            foreach (KeyValuePair<CamProc, PropPair> p in Properties)
            {
                p.Value.LoadSetting();
            }
        }

        public void InitPoperties()
        {
            Properties.Clear();
            Properties.Add(CamProc.Backlight, new PropPair("Backlight"));
            Properties.Add(CamProc.Brightness, new PropPair("Brightness"));
            Properties.Add(CamProc.Contrast, new PropPair("Contrast"));
            Properties.Add(CamProc.Exposure, new PropPair("Exposure"));
            Properties.Add(CamProc.Focus, new PropPair("Focus"));
            Properties.Add(CamProc.Gain, new PropPair("Gain"));
            Properties.Add(CamProc.Gamma, new PropPair("Gamma"));
            Properties.Add(CamProc.Hue, new PropPair("Hue"));
            Properties.Add(CamProc.Saturation, new PropPair("Saturation"));
            Properties.Add(CamProc.Sharpness, new PropPair("Sharpness"));
            Properties.Add(CamProc.WhiteBalance, new PropPair("WhiteBalance"));
            LoadPopertiesFromIni();
        }

        public void GetDefaultCamPropsAsync()
        {
            Task.Factory.StartNew(()=> GetDefaultCamProps());
        }

        public void GetDefaultCamProps()
        {
            int minValue = 0;
            int maxValue = 0;
            int stepSize = 0;
            int defaultValue = 0;

            if (Properties.ContainsKey(CamProc.Backlight))
            {      
                VideoProcAmpFlags flag = new VideoProcAmpFlags();               
                _camera.GetVideoPropertyRange(
                    VideoProcAmpProperty.BacklightCompensation,
                    out minValue,
                    out maxValue,
                    out stepSize,
                    out defaultValue,
                    out flag);

                Properties[CamProc.Backlight].Value = defaultValue;
                Properties[CamProc.Backlight].Min = minValue;
                Properties[CamProc.Backlight].Max = maxValue;
                Properties[CamProc.Backlight].Auto = (flag == VideoProcAmpFlags.Auto);
            }

            if (Properties.ContainsKey(CamProc.Brightness))
            {
                VideoProcAmpFlags flag = new VideoProcAmpFlags();
                _camera.GetVideoPropertyRange(
                    VideoProcAmpProperty.Brightness,
                    out minValue,
                    out maxValue,
                    out stepSize,
                    out defaultValue,
                    out flag);

                Properties[CamProc.Brightness].Value = defaultValue;
                Properties[CamProc.Brightness].Min = minValue;
                Properties[CamProc.Brightness].Max = maxValue;
                Properties[CamProc.Brightness].Auto = (flag == VideoProcAmpFlags.Auto);
            }

            if (Properties.ContainsKey(CamProc.Contrast))
            {
                VideoProcAmpFlags flag = new VideoProcAmpFlags();
                _camera.GetVideoPropertyRange(
                    VideoProcAmpProperty.Contrast,
                    out minValue,
                    out maxValue,
                    out stepSize,
                    out defaultValue,
                    out flag);

                Properties[CamProc.Contrast].Value = defaultValue;
                Properties[CamProc.Contrast].Min = minValue;
                Properties[CamProc.Contrast].Max = maxValue;
                Properties[CamProc.Contrast].Auto = (flag == VideoProcAmpFlags.Auto);
            }

            if (Properties.ContainsKey(CamProc.Exposure))
            {
                CameraControlFlags flag = new CameraControlFlags();
                _camera.GetCameraPropertyRange(
                    CameraControlProperty.Exposure,
                    out minValue,
                    out maxValue,
                    out stepSize,
                    out defaultValue,
                    out flag);

                Properties[CamProc.Exposure].Value = defaultValue;
                Properties[CamProc.Exposure].Min = minValue;
                Properties[CamProc.Exposure].Max = maxValue;
                Properties[CamProc.Exposure].Auto = (flag == CameraControlFlags.Auto);
            }

            if (Properties.ContainsKey(CamProc.Focus))
            {
                CameraControlFlags flag = new CameraControlFlags();
                _camera.GetCameraPropertyRange(
                    CameraControlProperty.Focus,
                    out minValue,
                    out maxValue,
                    out stepSize,
                    out defaultValue,
                    out flag);

                Properties[CamProc.Focus].Value = defaultValue;
                Properties[CamProc.Focus].Min = minValue;
                Properties[CamProc.Focus].Max = maxValue;
                Properties[CamProc.Focus].Auto = (flag == CameraControlFlags.Auto);
            }

            if (Properties.ContainsKey(CamProc.Hue))
            {
                VideoProcAmpFlags flag = new VideoProcAmpFlags();
                _camera.GetVideoPropertyRange(
                    VideoProcAmpProperty.Hue,
                    out minValue,
                    out maxValue,
                    out stepSize,
                    out defaultValue,
                    out flag);

                Properties[CamProc.Hue].Value = defaultValue;
                Properties[CamProc.Hue].Min = minValue;
                Properties[CamProc.Hue].Max = maxValue;
                Properties[CamProc.Hue].Auto = (flag == VideoProcAmpFlags.Auto);
            }

            if (Properties.ContainsKey(CamProc.Gain))
            {
                VideoProcAmpFlags flag = new VideoProcAmpFlags();
                _camera.GetVideoPropertyRange(
                    VideoProcAmpProperty.Gain,
                    out minValue,
                    out maxValue,
                    out stepSize,
                    out defaultValue,
                    out flag);

                Properties[CamProc.Gain].Value = defaultValue;
                Properties[CamProc.Gain].Min = minValue;
                Properties[CamProc.Gain].Max = maxValue;
                Properties[CamProc.Gain].Auto = (flag == VideoProcAmpFlags.Auto);
            }

            if (Properties.ContainsKey(CamProc.Gamma))
            {
                VideoProcAmpFlags flag = new VideoProcAmpFlags();
                _camera.GetVideoPropertyRange(
                    VideoProcAmpProperty.Gamma,
                    out minValue,
                    out maxValue,
                    out stepSize,
                    out defaultValue,
                    out flag);

                Properties[CamProc.Gamma].Value = defaultValue;
                Properties[CamProc.Gamma].Min = minValue;
                Properties[CamProc.Gamma].Max = maxValue;
                Properties[CamProc.Gamma].Auto = (flag == VideoProcAmpFlags.Auto);
            }

            if (Properties.ContainsKey(CamProc.Saturation))
            {
                VideoProcAmpFlags flag = new VideoProcAmpFlags();
                _camera.GetVideoPropertyRange(
                    VideoProcAmpProperty.Saturation,
                    out minValue,
                    out maxValue,
                    out stepSize,
                    out defaultValue,
                    out flag);

                Properties[CamProc.Saturation].Value = defaultValue;
                Properties[CamProc.Saturation].Min = minValue;
                Properties[CamProc.Saturation].Max = maxValue;
                Properties[CamProc.Saturation].Auto = (flag == VideoProcAmpFlags.Auto);
            }

            if (Properties.ContainsKey(CamProc.Sharpness))
            {
                VideoProcAmpFlags flag = new VideoProcAmpFlags();
                _camera.GetVideoPropertyRange(
                    VideoProcAmpProperty.Sharpness,
                    out minValue,
                    out maxValue,
                    out stepSize,
                    out defaultValue,
                    out flag);

                Properties[CamProc.Sharpness].Value = defaultValue;
                Properties[CamProc.Sharpness].Min = minValue;
                Properties[CamProc.Sharpness].Max = maxValue;
                Properties[CamProc.Sharpness].Auto = (flag == VideoProcAmpFlags.Auto); 
            }

            if (Properties.ContainsKey(CamProc.WhiteBalance))
            {
                VideoProcAmpFlags flag = new VideoProcAmpFlags();
                _camera.GetVideoPropertyRange(
                    VideoProcAmpProperty.WhiteBalance,
                    out minValue,
                    out maxValue,
                    out stepSize,
                    out defaultValue,
                    out flag);

                Properties[CamProc.WhiteBalance].Value = defaultValue;
                Properties[CamProc.WhiteBalance].Min = minValue;
                Properties[CamProc.WhiteBalance].Max = maxValue;
                Properties[CamProc.WhiteBalance].Auto = (flag == VideoProcAmpFlags.Auto);
            }       
        }

        public void SaveDefaultCamProps()
        {
            foreach(KeyValuePair<CamProc, PropPair> p in Properties)
            {
                p.Value.SaveSetting();
            }
        }

        public void GetCurrCamPropValueAsync()
        {
            Task.Factory.StartNew(()=> GetCurrCamPropValue());
        }

        public void GetCurrCamPropValue()
        {
            int nValue = 0;
            VideoProcAmpFlags flag = VideoProcAmpFlags.None;
            CameraControlFlags flagCC = CameraControlFlags.None;
            if (Properties.ContainsKey(CamProc.Backlight))
            {
                flag = VideoProcAmpFlags.None;
                _camera.GetVideoProperty(VideoProcAmpProperty.BacklightCompensation, out nValue, out flag);
                Properties[CamProc.Backlight].Value = nValue;
                Properties[CamProc.Backlight].Auto = (flag == VideoProcAmpFlags.Auto);
            }

            if (Properties.ContainsKey(CamProc.Brightness))
            {
                flag = VideoProcAmpFlags.None;
                _camera.GetVideoProperty(VideoProcAmpProperty.Brightness, out nValue, out flag);
                Properties[CamProc.Brightness].Value = nValue;
                Properties[CamProc.Brightness].Auto = (flag == VideoProcAmpFlags.Auto);
            }

            if (Properties.ContainsKey(CamProc.Contrast))
            {
                flag = VideoProcAmpFlags.None;
                _camera.GetVideoProperty(VideoProcAmpProperty.Contrast, out nValue, out flag);
                Properties[CamProc.Contrast].Value = nValue;
                Properties[CamProc.Contrast].Auto = (flag == VideoProcAmpFlags.Auto);
            }

            if (Properties.ContainsKey(CamProc.Exposure))
            {
                flagCC = CameraControlFlags.None;
                _camera.GetCameraProperty(CameraControlProperty.Exposure, out nValue, out flagCC);
                Properties[CamProc.Exposure].Value = nValue;
                Properties[CamProc.Exposure].Auto = (flagCC == CameraControlFlags.Auto);
            }

            if (Properties.ContainsKey(CamProc.Gain))
            {
                flag = VideoProcAmpFlags.None;
                _camera.GetVideoProperty(VideoProcAmpProperty.Gain, out nValue, out flag);
                Properties[CamProc.Gain].Value = nValue;
                Properties[CamProc.Gain].Auto = (flag == VideoProcAmpFlags.Auto);
            }

            if (Properties.ContainsKey(CamProc.Gamma))
            {
                flag = VideoProcAmpFlags.None;
                _camera.GetVideoProperty(VideoProcAmpProperty.Gamma, out nValue, out flag);
                Properties[CamProc.Gamma].Value = nValue;
                Properties[CamProc.Gamma].Auto = (flag == VideoProcAmpFlags.Auto);
            }

            if (Properties.ContainsKey(CamProc.Saturation))
            {
                flag = VideoProcAmpFlags.None;
                _camera.GetVideoProperty(VideoProcAmpProperty.Saturation, out nValue, out flag);
                Properties[CamProc.Saturation].Value = nValue;
                Properties[CamProc.Saturation].Auto = (flag == VideoProcAmpFlags.Auto);
            }

            if (Properties.ContainsKey(CamProc.Sharpness))
            {
                flag = VideoProcAmpFlags.None;
                _camera.GetVideoProperty(VideoProcAmpProperty.Sharpness, out nValue, out flag);
                Properties[CamProc.Sharpness].Value = nValue;
                Properties[CamProc.Sharpness].Auto = (flag == VideoProcAmpFlags.Auto);
            }

            if (Properties.ContainsKey(CamProc.Hue))
            {
                flag = VideoProcAmpFlags.None;
                _camera.GetVideoProperty(VideoProcAmpProperty.Hue, out nValue, out flag);
                Properties[CamProc.Hue].Value = nValue;
                Properties[CamProc.Hue].Auto = (flag == VideoProcAmpFlags.Auto);
            }

            if (Properties.ContainsKey(CamProc.WhiteBalance))
            {
                flag = VideoProcAmpFlags.None;
                _camera.GetVideoProperty(VideoProcAmpProperty.WhiteBalance, out nValue, out flag);
                Properties[CamProc.WhiteBalance].Value = nValue;
                Properties[CamProc.WhiteBalance].Auto = (flag == VideoProcAmpFlags.Auto);
            }

            if (Properties.ContainsKey(CamProc.Focus))
            {
                flagCC = CameraControlFlags.None;
                _camera.GetCameraProperty(CameraControlProperty.Focus, out nValue, out flagCC);
                Properties[CamProc.Focus].Value = nValue;
                Properties[CamProc.Focus].Auto = (flagCC == CameraControlFlags.Auto);
            }
        }

        public void SaveCurrCamPropValue()
        {
            foreach (KeyValuePair<CamProc, PropPair> p in Properties)
            {
                p.Value.SaveCurrentValue();
            }
        }

        public PropPair this[CamProc cp]
        {
            get
            {
                if (Properties.ContainsKey(cp))
                    return Properties[cp];
                else
                    return null;
            }
            set
            {
                Properties[cp] = value;
            }
        }

        public string[] StringValues()
        {
            string[] strArray = new string[3];

            strArray[0] = string.Format("亮度: {0}, 对比度: {1}, 色调: {2}, 饱和度: {3}",
                Properties[CamProc.Brightness].Value,
                Properties[CamProc.Contrast].Value,
                Properties[CamProc.Hue].Value,
                Properties[CamProc.Saturation].Value);

            strArray[1] = string.Format("清晰度: {0}, 白平衡: {1}, 增益（Gamma）: {2}",
              Properties[CamProc.Sharpness].Value,
              Properties[CamProc.WhiteBalance].Value,
              Properties[CamProc.Gamma].Value);

            strArray[2] = string.Format("对焦: {0}, 曝光: {1}, 逆光对比: {2}",
               Properties[CamProc.Focus].Value,
               Properties[CamProc.Exposure].Value,
               Properties[CamProc.Backlight].Value);

            return strArray;
        }

    }
}
